/**
 *
 */

package com.moogene.saad.gameplay.defender;

import android.graphics.Bitmap;
import android.graphics.Canvas;

import com.moogene.android.InfoMediator;
import com.moogene.android.R;
import com.moogene.android.engine.animlite.Animlite;
import com.moogene.android.engine.animlite.RotateAnim;
import com.moogene.saad.gameplay.attacter.Attacker;
import com.moogene.saad.gameplay.data.IDataHandler;

/**
 * @author 28849218
 */
public class DefAction implements Cloneable, IDataHandler {
	private Animlite anim;

	private int[] idleFrames;

	private int[] alertFrames;

	private int[] fireFrames;

	public DefAction(Bitmap img, int row, int col, int anchorX, int anchorY,
			int[] idleFrames, int[] alertFrames, int[] fireFrames) {
		Animlite anim = new Animlite(img, row, col,anchorX, anchorY);
//		anim.setAnchor(anchorX, anchorY);
		this.anim = anim;
		this.idleFrames = idleFrames;
		this.alertFrames = alertFrames;
		this.fireFrames = fireFrames;
	}

	public static DefAction load(String name) {
		if (name.equalsIgnoreCase("actMouse1")) {
			Bitmap img = InfoMediator.getBitmap(R.drawable.animlite_tower1);
			int row = 1;
			int col = 2;
			int anchorX = 50;
			int anchorY = 31;
			int[] idleFrames = null;
			int[] alertFrames = null;
			int[] fireFrames = new int[] { 1, 0 };
			DefAction da = new DefAction(img, row, col, anchorX, anchorY,
					idleFrames, alertFrames, fireFrames);
			return da;
		}
		if (name.equalsIgnoreCase("actMouse2")) {
			Bitmap img = InfoMediator.getBitmap(R.drawable.animlite_tower2);
			int row = 1;
			int col = 1;
			int anchorX = 58;
			int anchorY = 36;
			int[] idleFrames = null;
			int[] alertFrames = null;
			int[] fireFrames = null;
			DefAction da = new DefAction(img, row, col, anchorX, anchorY,
					idleFrames, alertFrames, fireFrames);
			return da;
		}
		return null;
	}

	public void draw(Canvas c, int x, int y){
		if (anim != null) {
			anim.draw(c, x, y);
		}
	}

	public void fire() {
		if (fireFrames != null) {
			anim.getFrameAnim().start(fireFrames);
		}
	}

	public void allert(int myX, int myY, Attacker target) {
		if (alertFrames != null) {
			anim.getFrameAnim().start(alertFrames);
		}
		anim.getRotateAnim().setDegree(target.getX(), target.getY(), myX, myY);
//		double angrad = Math.atan2(target.getY() - myY, target.getX() - myX);
//		double degree = Math.toDegrees(angrad) + 180;
//		RotateAnim ra = anim.getRotateAnim();
//		ra.setDegree((float) degree);

	}

	public void idle() {
		if (idleFrames != null) {
			anim.getFrameAnim().start(idleFrames);
		}
	}
	
	public void fix(boolean isFixed){
		if(isFixed){
			anim.getAlphaAnim().setAlpha(0xFF);
		}else{
			anim.getAlphaAnim().setAlpha(0xA0);
		}
	}

	Animlite getAnimlite() {
		return anim;
	}

	@Override
	public void checkVersion() {
		// TODO Auto-generated method stub

	}

	@Override
	public void download() {
		// TODO Auto-generated method stub

	}

	@Override
	public void save() {
		// TODO Auto-generated method stub

	}

	@Override
	public void upload() {
		// TODO Auto-generated method stub

	}

	@Override
	public DefAction clone() {
		DefAction da = null;
		try {
			da = (DefAction) super.clone();
			da.anim = new Animlite(anim.getResource(), anim.getNumRows(),
					anim.getNumColumns(), anim.getAnchor().getAnchorX(), anim.getAnchor().getAnchorY());
		} catch (CloneNotSupportedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return da;
	}
}
